using System.Collections;
using System.Drawing;
using Bullet;
using PoolSystem;
using UnityEngine;
/*
 * 原地向玩家发射子弹
 */
namespace Enemy
{
    public class Enemy1 : BaseEnemy
    {
    
        private void Start()
        {
            Hp = MaxHp;
            
            bulletPrefab = Resources.Load<GameObject>("Prefabs/Enemy/Bullet/EnemyBullet1");
            point = transform.Find("point").gameObject;
            base.Start();
        }

        private void Update()
        {
            //Action();
            base.Update();
        }

        public override void Action()
        {
            // 面向敌人方向，只改变Y轴旋转
            if (Player.Player.instance != null)
            {
                Vector3 targetDirection = Player.Player.instance.transform.position - transform.position;
                targetDirection.y = 0; // 忽略Y轴方向

                Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
                transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, targetRotation.eulerAngles.y, transform.rotation.eulerAngles.z);
            }
        
            // 发射子弹
            if(Player.Player.instance != null)
            {
                if(Time.time >= nextAttackTime)
                {
                    StartCoroutine(CreateBullet());
                    nextAttackTime = Time.time + attackCooldown;
                }
            }
        }

        // ReSharper disable Unity.PerformanceAnalysis
        /// <summary>
        /// 创建子弹
        /// </summary>
        /// <returns></returns>
        IEnumerator CreateBullet()
        {
            /*GameObject bullet = Instantiate(bulletPrefab);
            //bullet的旋转和位置此脚本对象一致
            bullet.transform.rotation = transform.rotation;
            bullet.transform.position = transform.position;
            bullet.GetComponent<EnemyBullet>().AttackRange = AttackRange;
            yield return new WaitForSeconds(1f);*/
            //播放攻击动画
            anim.SetTrigger("attack");
            yield return new WaitForSeconds(0.5f);//因为动画有点慢，等待子弹一起出来（0.5s够了）

            //利用对象池来管理敌人子弹
            //GameObject bullet = PoolManager.Release(bulletPrefab, transform.position, transform.rotation);
            GameObject bullet = PoolManager.Release(bulletPrefab, point.transform.position, point.transform.rotation);
            bullet.GetComponent<EnemyBullet>().attackRange = AttackRange;
            yield return new WaitForSeconds(1f);
        }

        /// <summary>
        /// 针对玩家子弹的trigger
        /// </summary>
        /// <param name="other"></param>
        public void OnTriggerEnter(Collider other)
        {
            base.OnTriggerEnter(other);
            if (other.gameObject.tag == "Bullet")
            {
                GetHit();

            }
        }

    }
}

